package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfPackersLegalBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.packers.PackersLegalEntity;
import com.sgb.kjwl.view.ui.adapter.packers.PackersLegalAdapter;
import com.sgb.kjwl.viewmodel.self.packers.SelfLegalModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * description:个人中心-法务团队
 * @author 毁灭霸者
 * @date 2020/7/2
 */
public class SelfLegalActivity extends AppActivity {
    public static final int REQUEST_CODE =  101;
    @Inject
    SelfLegalModel viewModel;
    private ActivitySelfPackersLegalBinding binding;
    private PackersLegalAdapter packersLegalAdapter;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfLegalActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_self_packers_legal);
        binding.setViewModel(viewModel);

        if (packersLegalAdapter == null) {
            packersLegalAdapter = new PackersLegalAdapter(getActivity(), null);
        }
        packersLegalAdapter.setShowDel(true);
        packersLegalAdapter.setItemCallBack(this::optState);
        packersLegalAdapter.setOnItemClick(new AdapterOnItemClick<PackersLegalEntity>() {
            @Override
            public void onItemClick(PackersLegalEntity packersLegalEntity, int position) {
                viewModel.getH5Url(packersLegalEntity.getId(), packersLegalEntity.getNo());
            }
        });
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp10), ContextCompat.getColor(this,R.color.color_F5F5F5)));

        binding.recyclerView.setAdapter(packersLegalAdapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getLegalListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getLegalListModel(true);
            }
        });
    }

    @Override
    protected void init() {
        viewModel.getLegalListModel(false);
    }

    public void showList(List<PackersLegalEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                packersLegalAdapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            packersLegalAdapter.setDatas(list);
        }
    }

    private void optState(String id) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.delList(id);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                viewModel.getLegalListModel(false);
            }
        }

    }
}
